package question

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gvalid"
	"github.com/gogf/gfer/app/model/question"
)

// 问答写入/更新对象
type SaveParam struct {
	Id      uint
	Uid     uint   `v:"required#用户ID不能为空"`
	Title   string `v:"required#标题不能为空"`
	Content string `v:"required#内容不能为空"`
	Brief   string
	Thumb   string
	Referer string
	Tags    string
}

// 保存问答内容
func (p *SaveParam) Save() error {
	if err := gvalid.CheckStruct(p, nil); err != nil {
		return err
	}
	err := g.DB().Transaction(func(tx *gdb.TX) error {
		if _, err := question.Model.TX(tx).Save(p); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		panic(err)
	}
	return nil
}
